﻿using IQKS.AWP.BusinessAccessLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Assessor_CandidateCalendarView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Guid g1;
            CandidateId.Text = (string)(Session["CandidateID"]);
            CandidateBriefId.Text = (string)(Session["CandidateBriefID"]);
            CandidateName.Text = (string)(Session["Candidate"]);
            BriefId.Text = (string)(Session["BriefID"]);
            DataSet d1 = GetCandidateDetails(Convert.ToInt64(CandidateId.Text));
            g1 = new Guid(d1.Tables[0].Rows[0][1].ToString());
            UserId.Text = d1.Tables[0].Rows[0][1].ToString().ToUpper();
            StatusId.Text = d1.Tables[0].Rows[0][8].ToString();
            //g1 =  new Guid( d1.Tables[0].Rows[0][1].ToString());
            DataSet d2 = GetCandidateBriefDetails(g1, Convert.ToInt64(BriefId.Text));
            hdnCandidateGDID.Value = Convert.ToString(d2.Tables[4].Rows[0]["CandidateGDID"]);
            hdnPartnerID.Value = Convert.ToString(d2.Tables[0].Rows[0]["PartnerIntID"]);
            if (d2.Tables[5].Rows.Count > 0)
            {
                gvCandidateSelectAssessordate.DataSource = d2.Tables[5];
                gvCandidateSelectAssessordate.DataBind();

                //grdPropAssessorAvailable.DataSource = objDs.Tables[5];
                //grdPropAssessorAvailable.DataBind();

                //if (objDs.Tables[5].Rows.Count == 1)
                //{
                // if (hdnCandidateStatus.Value == "6")
                //{
                //   hdnCanGDDate.Value = Convert.ToString(objDs.Tables[5].Rows[0]["CanGDDate"]);
                // }
                //  }
            }
            DataSet d3 = GetAssessorCalendarUpdateLog(Convert.ToInt64(CandidateId.Text));
            if (d3.Tables.Count > 1)
            {
                if (d3.Tables[0].Rows.Count > 0)
                {
                    gvCandidateOldGD.DataSource = d3.Tables[0];
                    gvCandidateOldGD.DataBind();
                }
            } 
        }

    }
    private DataSet GetCandidateDetails(Int64 CandidateId)
    {
        DataSet objDs = null;
        try
        {
            //todo  CommonUtility.GetLoggedOnUserId()
            objDs = new UserMasterBAL().GetCandidateMasterNew(CandidateId);

        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    private DataSet GetCandidateBriefDetails(Guid UserId, Int64 CandidateBriefId)
    {
        DataSet objDs = null;
        try
        {
            //todo  CommonUtility.GetLoggedOnUserId()
            objDs = new CandidateBriefDetailBAL().GetCandidateBriefByCandidateID(UserId, CandidateBriefId);

        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    private DataSet GetAssessorCalendarUpdateLog(Int64 CandidateId)
    {
        DataSet objDs = null;
        try
        {
            //todo  CommonUtility.GetLoggedOnUserId()
            objDs = new CandidateBriefDetailBAL().GetAssessorCalendarUpdateLog(CandidateId);

        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strReturnValue = "";
        if (Page.IsValid == true)
        {
            try
            {
                CandidateBriefDetailBAL objCandidateBriefDetailBAL = new CandidateBriefDetailBAL();

                bool CandidateFirstHalfCheck = false;
                bool CandidateSecondHalfCheck = false;

                string GDTime = "";

                if (ddlAMPM.SelectedValue == "PM")
                {
                    if (ddlHour.SelectedValue == "12")
                    {
                        GDTime = "00" + ":" + ddlMinute.SelectedValue + ":00";

                    }
                    else
                    {
                        GDTime = (Convert.ToInt32(ddlHour.SelectedValue) + 12).ToString("00") + ":" + ddlMinute.SelectedValue + ":00";
                    }
                }
                else
                {
                    GDTime = ddlHour.SelectedValue + ":" + ddlMinute.SelectedValue + ":00";
                }

                int AssessorCalendarID = 0;

                foreach (GridViewRow item in gvCandidateSelectAssessordate.Rows)
                {
                    CheckBox chkApproveFirstHalf = (CheckBox)item.FindControl("chkApproveFirstHalf");
                    CheckBox chkApproveSecondHalf = (CheckBox)item.FindControl("chkApproveSecondHalf");
                    CheckBox chkCandidateFirstHalf = (CheckBox)item.FindControl("chkCandidateFirstHalf");
                    CheckBox chkCandidateSecondHalf = (CheckBox)item.FindControl("chkCandidateSecondHalf");
                    Label lblAssessorCalendarDetailID = (Label)item.FindControl("lblAssessorCalendarDetailID");

                    if (chkCandidateFirstHalf.Checked)
                    {
                        CandidateFirstHalfCheck = true;
                        AssessorCalendarID = lblAssessorCalendarDetailID.Text != "" ? Convert.ToInt32(lblAssessorCalendarDetailID.Text) : 0;
                    }
                    else if (chkCandidateSecondHalf.Checked)
                    {
                        CandidateSecondHalfCheck = true;
                        AssessorCalendarID = lblAssessorCalendarDetailID.Text != "" ? Convert.ToInt32(lblAssessorCalendarDetailID.Text) : 0;
                    }
                }

                //int GDCandidateID = hdnGDCandidateID.Value != "" ? Convert.ToInt32(hdnGDCandidateID.Value) : 0;
                int CandidateGDID = hdnCandidateGDID.Value != "" ? Convert.ToInt32(hdnCandidateGDID.Value) : 0;

                /*   @CandidateGDID BIGINT,
       @CandidateBriefID BIGINT,
       @CandidateID BIGINT,
       @BriefID BIGINT,

       @UserID UNIQUEIDENTIFIER,--loginid

       @GDCandidateTime TIME(7),
       @AssessorCalendarDetailID BIGINT,
       @FirstHalfCheck BIT,
       @SecondHalfCheck BIT*/


                strReturnValue = (string)objCandidateBriefDetailBAL.SetAssessorCandidateGroupDiscussion(CandidateGDID, CandidateBriefId.Text, CandidateId.Text, BriefId.Text,
                    CommonUtility.GetLoggedOnUserId(), GDTime, AssessorCalendarID, CandidateFirstHalfCheck, CandidateSecondHalfCheck);

            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error while saving Record : " + ex.Message;
            }
            finally
            {
                if (strReturnValue == "SUCCESS")
                {
                    //string strNewMsg="\n\n Please Scroll down to upload GS & Stimulus";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Candidate Group discussion Slot changed successfully.');", true);
                    //Response.Redirect("../home/Dashboard.aspx");
                    DataSet d3 = GetAssessorCalendarUpdateLog(Convert.ToInt64(CandidateId.Text));
                    if (d3.Tables.Count > 1)
                    {
                        if (d3.Tables[0].Rows.Count > 0)
                        {
                            gvCandidateOldGD.DataSource = d3.Tables[0];
                            gvCandidateOldGD.DataBind();
                        }
                    }

                }
                else if (strReturnValue == "PARTNERERROR")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Partner candidate still not completed proposal stage.');", true);
                }
                else if (strReturnValue == "ERROR")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Some Error, please contact IT.');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('"+strReturnValue+"');", true);
                }
            }

        }
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("../home/Dashboard.aspx");
    }

    protected void cstGDCandidateTime_ServerValidate(object source, ServerValidateEventArgs args)
    {

        string sHr = "";

        //validation required

        if (ddlAMPM.SelectedValue == "PM")
        {
            if (ddlHour.SelectedValue == "12")
            {
                sHr = (00).ToString("00");
            }
            else
            {
                sHr = (Convert.ToInt32(ddlHour.SelectedValue) + 12).ToString("00");

            }
        }
        else
        {
            sHr = ddlHour.SelectedValue;
        }

        string sMin = ddlMinute.SelectedValue;
        bool flag = false;

        foreach (GridViewRow item in gvCandidateSelectAssessordate.Rows)
        {
            CheckBox chkApproveFirstHalf = (CheckBox)item.FindControl("chkApproveFirstHalf");
            CheckBox chkApproveSecondHalf = (CheckBox)item.FindControl("chkApproveSecondHalf");
            CheckBox chkCandidateFirstHalf = (CheckBox)item.FindControl("chkCandidateFirstHalf");
            CheckBox chkCandidateSecondHalf = (CheckBox)item.FindControl("chkCandidateSecondHalf");

            Label lblFHHour = (Label)item.FindControl("lblFHHour");
            Label lblFHMinute = (Label)item.FindControl("lblFHMinute");
            Label lblSHHour = (Label)item.FindControl("lblSHHour");
            Label lblSHMinute = (Label)item.FindControl("lblSHMinute");

            if (chkApproveFirstHalf.Checked && chkApproveSecondHalf.Checked && chkCandidateFirstHalf.Checked && chkCandidateSecondHalf.Checked)
            {
                flag = true;
            }
            else if (chkApproveFirstHalf.Checked && chkApproveSecondHalf.Checked && ((chkCandidateFirstHalf.Checked) || (chkCandidateSecondHalf.Checked)))
            {
                if (chkCandidateFirstHalf.Checked == true)
                {
                    TimeSpan dtCandidateTime = TimeSpan.Parse(sHr + ":" + sMin + ":00");
                    TimeSpan dtFirst = TimeSpan.Parse(lblFHHour.Text + ":" + lblFHMinute.Text + ":00");
                    TimeSpan dtFirstHalf = TimeSpan.Parse("12:00:00");

                    if (dtCandidateTime >= dtFirst && dtCandidateTime <= dtFirstHalf)
                    {
                        flag = true;
                    }
                }

                if (chkCandidateSecondHalf.Checked == true)
                {
                    TimeSpan dtCandidateTime = TimeSpan.Parse(sHr + ":" + sMin + ":00");

                    string SHHour = "";

                    if (ddlHour.SelectedValue == "12")
                    {
                        SHHour = (00).ToString("00");
                    }
                    else
                    {
                        SHHour = (Convert.ToInt32(lblSHHour.Text) + 12).ToString("00");
                    }

                    TimeSpan dtSecond = TimeSpan.Parse(SHHour + ":" + lblSHMinute.Text + ":00");
                    TimeSpan dtSecondHalf = TimeSpan.Parse("23:55:00");

                    if (dtCandidateTime >= dtSecond && dtCandidateTime <= dtSecondHalf)
                    {
                        flag = true;
                    }
                }

            }
            else if (chkApproveFirstHalf.Checked && chkCandidateFirstHalf.Checked)
            {
                TimeSpan dtCandidateTime = TimeSpan.Parse(sHr + ":" + sMin + ":00");
                TimeSpan dtFirst = TimeSpan.Parse(lblFHHour.Text + ":" + lblFHMinute.Text + ":00");
                TimeSpan dtFirstHalf = TimeSpan.Parse("12:00:00");

                if (dtCandidateTime >= dtFirst && dtCandidateTime <= dtFirstHalf)
                {
                    flag = true;
                }
            }
            else if (chkApproveSecondHalf.Checked && chkCandidateSecondHalf.Checked)
            {
                TimeSpan dtCandidateTime = TimeSpan.Parse(sHr + ":" + sMin + ":00");

                string SHHour = "";

                if (ddlHour.SelectedValue == "12")
                {
                    SHHour = (00).ToString("00");
                }
                else
                {
                    SHHour = (Convert.ToInt32(lblSHHour.Text) + 12).ToString("00");
                }

                TimeSpan dtSecond = TimeSpan.Parse(SHHour + ":" + lblSHMinute.Text + ":00");
                TimeSpan dtSecondHalf = TimeSpan.Parse("23:55:00");

                if (dtCandidateTime >= dtSecond && dtCandidateTime <= dtSecondHalf)
                {
                    flag = true;
                }
            }
        }


        if (!flag)
        {
            args.IsValid = false;
            cstAssessorDate.ErrorMessage = "Select Time As Per Assessor Availability";
            return;
        }
    }
    protected void gvCandidateSelectAssessordate_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataSet objDs = new DataSet();
        objDs = GetCandidateBriefDetails(new Guid(UserId.Text), Convert.ToInt64(BriefId.Text));
        gvCandidateSelectAssessordate.PageIndex = e.NewPageIndex;
        gvCandidateSelectAssessordate.DataSource = objDs.Tables[5];
        gvCandidateSelectAssessordate.DataBind();
    }
    protected void gvCandidateSelectAssessordate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int rwIndex = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            rwIndex = e.Row.RowIndex + (gvCandidateSelectAssessordate.PageSize * gvCandidateSelectAssessordate.PageIndex);
            DataTable DtObj = (DataTable)gvCandidateSelectAssessordate.DataSource;
            CheckBox chkApproveFirstHalf = (CheckBox)e.Row.FindControl("chkApproveFirstHalf");
            CheckBox chkApproveSecondHalf = (CheckBox)e.Row.FindControl("chkApproveSecondHalf");
            CheckBox chkCandidateFirstHalf = (CheckBox)e.Row.FindControl("chkCandidateFirstHalf");
            CheckBox chkCandidateSecondHalf = (CheckBox)e.Row.FindControl("chkCandidateSecondHalf");
            Label lblFHHour = (Label)e.Row.FindControl("lblFHHour");
            Label lblFHMinute = (Label)e.Row.FindControl("lblFHMinute");
            Label lblSHHour = (Label)e.Row.FindControl("lblSHHour");
            Label lblSHMinute = (Label)e.Row.FindControl("lblSHMinute");
            Label lblAssessorCalendarDetailID = (Label)e.Row.FindControl("lblAssessorCalendarDetailID");

            chkApproveFirstHalf.Checked = Convert.ToBoolean(DtObj.Rows[rwIndex]["FirstHalfCheck"]);
            chkApproveSecondHalf.Checked = Convert.ToBoolean(DtObj.Rows[rwIndex]["SecondHalfCheck"]);
            lblAssessorCalendarDetailID.Text = DtObj.Rows[rwIndex]["AssessorCalendarDetailID"].ToString();

            /*if (StatusId.Text == "6")
            {*/
                chkCandidateFirstHalf.Enabled = false;
                chkCandidateSecondHalf.Enabled = false;
            /*}
            else
            {
                chkCandidateFirstHalf.Enabled = false;
                chkCandidateSecondHalf.Enabled = false;
            }*/


            lblFHHour.Text = TimeSpan.Parse(DtObj.Rows[rwIndex]["FirstHalfTime"].ToString()).Hours.ToString("00");
            lblFHMinute.Text = TimeSpan.Parse(DtObj.Rows[rwIndex]["FirstHalfTime"].ToString()).Minutes.ToString("00");
            lblSHHour.Text = CommonUtility.GetHour(TimeSpan.Parse(DtObj.Rows[rwIndex]["SecondHalfTime"].ToString()).Hours).ToString("00");
            lblSHMinute.Text = TimeSpan.Parse(DtObj.Rows[rwIndex]["SecondHalfTime"].ToString()).Minutes.ToString("00");

            chkCandidateFirstHalf.Checked = Convert.ToBoolean(DtObj.Rows[rwIndex]["CandidateFirstHalfCheck"]);
            chkCandidateSecondHalf.Checked = Convert.ToBoolean(DtObj.Rows[rwIndex]["CandidateSecondHalfCheck"]);

            if (StatusId.Text == "6")
            {
                DateTime NewDate = Convert.ToDateTime(DtObj.Rows[rwIndex]["FirstHalfTime"].ToString()).AddHours(1);
                chkCandidateFirstHalf.Attributes.Add("Time", NewDate.ToString("hh:mm:tt"));
                chkCandidateFirstHalf.Enabled = chkApproveFirstHalf.Checked;
                if (chkApproveFirstHalf.Checked == false)
                    chkCandidateFirstHalf.Style.Add("display", "none");

                NewDate = Convert.ToDateTime(DtObj.Rows[rwIndex]["SecondHalfTime"].ToString()).AddHours(1);
                chkCandidateSecondHalf.Attributes.Add("Time", NewDate.ToString("hh:mm:tt"));
                chkCandidateSecondHalf.Enabled = chkApproveSecondHalf.Checked;
                if (chkApproveSecondHalf.Checked == false)
                    chkCandidateSecondHalf.Style.Add("display", "none");
            }
            chkApproveFirstHalf.Style.Add("display", "none");
            chkApproveSecondHalf.Style.Add("display", "none");
            chkApproveFirstHalf.Enabled = false;
            chkApproveSecondHalf.Enabled = false;

        }
    }
    protected void cstAssessorDate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (gvCandidateSelectAssessordate.Rows.Count > 0)
        {
            bool flag = false;
            int iCnt = 0;

            CandidateBriefDetailBAL objCandidateBriefDetailBAL = new CandidateBriefDetailBAL();

            foreach (GridViewRow item in gvCandidateSelectAssessordate.Rows)
            {
                CheckBox chkApproveFirstHalf = (CheckBox)item.FindControl("chkApproveFirstHalf");
                CheckBox chkApproveSecondHalf = (CheckBox)item.FindControl("chkApproveSecondHalf");
                CheckBox chkCandidateFirstHalf = (CheckBox)item.FindControl("chkCandidateFirstHalf");
                CheckBox chkCandidateSecondHalf = (CheckBox)item.FindControl("chkCandidateSecondHalf");
                Label lblAssessorCalendarDetailID = (Label)item.FindControl("lblAssessorCalendarDetailID");

                if (chkCandidateFirstHalf.Checked || chkCandidateSecondHalf.Checked)
                {
                    flag = true;
                    iCnt++;

                    /*   if (Convert.ToInt64(hdnPartnerID.Value) > 0)
                       {
                           if (objCandidateBriefDetailBAL.GetAssessorAvailability(Convert.ToInt32(lblAssessorCalendarDetailID.Text), 1, 0, "AM", Convert.ToInt64(hdnCandidateID.Value), Convert.ToInt64(hdnBriefID.Value)).Tables[0].Rows.Count > 0)
                           {
                               args.IsValid = false;
                               cstAssessorDate.ErrorMessage = "Assessor Already Assigned To Other Candidate For Selected Time.";
                               return;
                           }
                       }
                       else*/
                    {
                        if (chkCandidateFirstHalf.Checked)
                        {
                            if (objCandidateBriefDetailBAL.GetAssessorAvailability(Convert.ToInt32(lblAssessorCalendarDetailID.Text), 1, 0, "AM", Convert.ToInt64(CandidateId.Text), Convert.ToInt64(CandidateBriefId.Text)).Tables[0].Rows.Count > 0)
                            {
                                args.IsValid = false;
                                cstAssessorDate.ErrorMessage = "Assessor Already Assigned To Other Candidate For Selected Time.";
                                return;
                            }
                        }

                        if (chkCandidateSecondHalf.Checked)
                        {
                            if (objCandidateBriefDetailBAL.GetAssessorAvailability(Convert.ToInt32(lblAssessorCalendarDetailID.Text), 0, 1, "PM", Convert.ToInt64(CandidateId.Text), Convert.ToInt64(CandidateBriefId.Text)).Tables[0].Rows.Count > 0)
                            {
                                args.IsValid = false;
                                cstAssessorDate.ErrorMessage = "Assessor Already Assigned To Other Candidate For Selected Time.";
                                return;
                            }

                        }
                    }


                }
            }
            if (!flag)
            {
                args.IsValid = false;
                cstAssessorDate.ErrorMessage = "Select Atleast One Time";
                return;
            }
            if (iCnt != 1)
            {
                args.IsValid = false;
                cstAssessorDate.ErrorMessage = "Select Only One Day";
                return;
            }
        }

    }
    protected void cstGDPartnerValidation_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Convert.ToInt64(hdnPartnerID.Value) > 0)
        {
            DataSet objDs = new DataSet();
            objDs = new CandidateBriefDetailBAL().GetGDPartnerValidation(hdnPartnerID.Value);

            if (Convert.ToInt64(objDs.Tables[0].Rows[0][0]) <= 0)
            {
                args.IsValid = false;
                cstGDPartnerValidation.ErrorMessage = "Perfered partner candidate still not completed proposal stage.";
                return;
            }
        }
    }
}